<!--
 * @Descripttion: 模具类型表单
 * @LastEditors: xzh
 * @LastEditTime: 2023-09-11 20:25:16
-->
<template>
  <DileDrawer
    v-model="moldTypeForm.visible"
    :loading="moldTypeForm.loading"
    size="40%"
    @sure="hanldeSure"
  >
    <template #header>
      <span>{{
        $t(`table.btnArr.${!moldTypeForm.formData.id ? "add" : "edit"}`)
      }}</span>
    </template>
    <template #default>
      <el-form
        ref="formRef"
        :model="moldTypeForm.formData"
        :rules="formRules"
        label-width="7rem"
        size="large"
        status-icon
      >
        <el-form-item
          :label="$t('page.moldManageArr.moldTypeArr.parentType')"
          :validate-status="!!moldTypeForm.formData.parentId ? 'success' : ''"
        >
          <!-- 输入框选择对象 -->
          <DileInputSelectItem
            v-model="moldTypeForm.formData.parentId"
            :expand-on-click-node="false"
            :selectType="selectTypeEnum.moldType"
            :cacheLabel="moldTypeForm.formData.extJson.parentName"
            :filterIds="[moldTypeForm.formData.id]"
          />
        </el-form-item>
        <el-form-item
          :label="$t('page.moldManageArr.moldTypeArr.name')"
          prop="name"
        >
          <el-input v-model="moldTypeForm.formData.name" />
        </el-form-item>
        <el-form-item :label="$t('page.moldManageArr.moldTypeArr.code')">
          <el-input
            v-model="moldTypeForm.formData.code"
            disabled
            :placeholder="$t('common.systemAutoGenerate')"
          />
        </el-form-item>

        <el-form-item :label="$t('common.status')">
          <el-radio-group v-model="moldTypeForm.formData.status">
            <el-radio :label="true">{{ $t("table.btnArr.enable") }}</el-radio>
            <el-radio :label="false">{{
              $t("table.btnArr.disEnable")
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('common.remark')">
          <el-input
            v-model="moldTypeForm.formData.remark"
            :rows="5"
            type="textarea"
          />
        </el-form-item>
      </el-form>
    </template>
  </DileDrawer>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, computed, nextTick } from "vue";
import type { object_Inf } from "@/utils/typings/index.type";
import { useI18n } from "vue-i18n";
import { ElNotification } from "element-plus";
import { selectTypeEnum } from "@/components/dile-selectItem/index.type";
import DileInputSelectItem from "@/components/dile-selectItem/selectItem-input/index.vue";
import DileDrawer from "@/components/dile-drawer/index.vue";
import Dile_APi from "@/api";

const emit = defineEmits(["refresh"]);

let { t } = useI18n();

/**
 * @Author: xzh
 * @Descripttion: 表单默认数据
 * @Param:
 */
let defaultData = () => ({
  id: 0,
  parentId: null, //上级类型
  code: "", //模具类型编码
  name: "", //模具类型名称
  status: true, //状态
  remark: "", //备注
  extJson: {
    parentName: "", //上级类型名称
  },
});
/**
 * @Author: xzh
 * @Descripttion: 部门表单参数
 * @Param:
 */
let moldTypeForm = reactive<object_Inf>({
  visible: false,
  loading: false,
  //表单数据
  formData: defaultData(),
});

/**
 * @Author: xzh
 * @Descripttion: 表单实例
 * @Param:
 */
let formRef = ref<any>(null);

/**
 * @Author: xzh
 * @Descripttion:表单验证规则
 * @Param:
 * @param {*} computed
 */
let formRules = computed((): any => {
  return reactive({
    name: [
      {
        required: true,
        message: t("common.enter") + t("page.moldManageArr.moldTypeArr.name"),
        trigger: "change",
      },
    ],
  });
});

/**
 * @Author: xzh
 * @Descripttion:初始化加载
 * @Param:
 * @param {*} id 部门主键
 */
let init = async (id: number) => {
  let { visible, loading } = toRefs(moldTypeForm);
  visible.value = true;
  await nextTick();
  try {
    loading.value = true;
    //加载表单数据
    await initFormData(id);
  } finally {
    loading.value = false;
  }
};

/**
 * @Author: xzh
 * @Descripttion: 加载表单数据
 * @Param:
 */
let initFormData = async (id: number) => {
  let data: object_Inf = !!id
    ? await Dile_APi.getMoldTypeInfo(id)
    : defaultData();
  Object.assign(moldTypeForm.formData, data);
};

/**
 * @Author: xzh
 * @Descripttion: 保存事件
 * @Param:
 */
let hanldeSure = async () => {
  let { formData, loading, visible } = toRefs(moldTypeForm),
    valid = await formRef.value.validate();
  if (!valid || !!loading.value) {
    return false;
  }
  try {
    loading.value = true;
    await Dile_APi.saveMoldType(formData.value);
    //触发刷新回调
    emit("refresh");
    //提示成功
    ElNotification({
      message: t("common.message.OperationSuccess"),
      type: "success",
    });
    //关闭弹框
    visible.value = false;
  } finally {
    loading.value = false;
  }
};

defineExpose({
  init,
});
</script>
<style lang="scss" scoped></style>
